﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 承运商管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CarrierController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        public ICarrierRepository CarrierService { get; set; }
        public CarrierController(ICarrierRepository carrierservice, ILogRepository logRepository)
        {
            CarrierService = carrierservice;
            this.logRepository = logRepository;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCarriers(string? name, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount = 0;
            var list = CarrierService.GetCarriers(name, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(list, "查询成功", "200", totalCount));
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetCarrier(int ids)
        {
            var car = CarrierService.GetCarrier(ids);
            return Ok(BaseResult.Result(car, "查询成功", "200"));
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddCarrier(CarrierDto dto)
        {
            if (string.IsNullOrEmpty(dto.Code) || dto.Code.Length <= 0)
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            string uname = HttpContext.User.FindFirstValue("UserName");
            string runame = HttpContext.User.FindFirstValue("RealName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            dto.CreateUserId = uid;
            dto.CreateUserName = uname;
            bool isko = CarrierService.AddCarrier(dto);
            if (isko)
            {    
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了承运商" + dto.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！编码已存在！", "500"));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateCarrier(CarrierDto dto)
        {
            if (dto.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(dto.Code))
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            string uname = HttpContext.User.FindFirstValue("UserName");
            string runame = HttpContext.User.FindFirstValue("RealName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            dto.ModifyUserId = uid;
            dto.ModifyUserName = uname;
            bool isko = CarrierService.UpdateCarrier(dto);
            if (isko)
            {          
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了承运商" + dto.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }

        /// <summary>
        /// 删除承运商
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelCarrier(int ids)
        {
            if(ids<=0)
            {      
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool isko = CarrierService.DelCarrier(ids);
            if (isko)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了承运商";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！编码已存在！", "500"));
        }
        /// <summary>
        /// 批量删除承运商
        /// </summary>
        /// <param name="idsList"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult DelCarrierList(int[] idsList)
        {
            if (idsList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            foreach (int ids in idsList) 
            {
                bool isko = CarrierService.DelCarrier(ids);
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = isko ? uname + "删除了承运商" : uname + "删除承运商失败";
                //获取前端IP地址
                logg.Ipaddress = ip;
                logRepository.AddTLod(logg);
                if (!isko)
                {
                    daRes = false;
                    break;
                }
            }
            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！编码已存在！", "500"));
        }

    }
}
